package net.gaox.service.impl;

import net.gaox.entrty.BankAccount;
import net.gaox.service.BankInterface;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p> 银行卡操作实现类 </p>
 *
 * @author gaox·Eric
 * @date 2021/11/11 20:45
 */
public class BankInterfaceImpl implements BankInterface {
    @Override
    public Boolean register(Long id, String accountNumber, String name) {
        // 参数校验
        if ((null == id)
                && (null == accountNumber && "".equals(accountNumber))
                && (null == accountNumber && "".equals(accountNumber))) {
            return false;
        }
        List<BankAccount> list = getBankAccounts();
        // 银行卡号是否重复
        for (int i = 0; i < list.size(); i++) {
            BankAccount bankAccount = list.get(i);
            // 相同id
            if (bankAccount.getId().equals(id)) {
                return false;
            }
            // 相同银行卡号
            if (bankAccount.getAccountNumber().equals(accountNumber)) {
                return false;
            }
        }
        // 构建账户，并添加到银行卡列表
        new BankAccount(id, accountNumber, name);
        return true;
    }

    @Override
    public Boolean cancel(String accountNumber) {
        List<BankAccount> list = getBankAccounts();
        for (int i = 0; i < list.size(); i++) {
            BankAccount bankAccount = list.get(i);
            // 根据银行卡号查找到银行卡
            if (bankAccount.getAccountNumber().equals(accountNumber)) {
                // 注销时，余额应为0
                if (0 == BigDecimal.ZERO.compareTo(bankAccount.getBalance())) {
                    bankAccount.setStatus(false);
                    bankAccount.setDisableTime(LocalDateTime.now());
                    // 注销成功
                    return true;
                } else {
                    return false;
                }
            }
        }
        return null;
    }

    @Override
    public Boolean addMoney(String accountNumber, BigDecimal balance) {
        Optional<BankAccount> first = getBankAccounts().stream()
                // 查找相同的银行卡号
                .filter(s -> s.getAccountNumber().equals(accountNumber))
                .findFirst();
        if (first.isPresent()) {
            BankAccount bankAccount = first.get();
            if (bankAccount.getStatus()) {
                // 原始余额
                BigDecimal oldBalance = bankAccount.getBalance();
                // 变更后的余额
                BigDecimal newBalance = oldBalance.add(balance);
                bankAccount.setBalance(newBalance);
                return true;
            } else {
                return false;
            }
        }
        // 没有找到对应银行卡
        return null;
    }

    @Override
    public Boolean subMoney(String accountNumber, BigDecimal balance) {
        Optional<BankAccount> first = getBankAccounts().stream()
                // 查找相同的银行卡号
                .filter(s -> s.getAccountNumber().equals(accountNumber))
                .findFirst();
        if (first.isPresent()) {
            BankAccount bankAccount = first.get();
            if (bankAccount.getStatus()) {
                // 原始余额
                BigDecimal oldBalance = bankAccount.getBalance();
                // 预取款，获得余额
                BigDecimal newBalance = oldBalance.subtract(balance);
                // 取钱金额不能超过余额
                // if (0 > newBalance.compareTo(BigDecimal.ZERO)) {
                if (0 < BigDecimal.ZERO.compareTo(newBalance)) {
                    return false;
                }
                // 变更后的余额
                bankAccount.setBalance(newBalance);
                return true;
            } else {
                return false;
            }
        }
        // 没有找到对应银行卡
        return null;
    }

    @Override
    public BankAccount getBankAccount(String accountNumber) {
        List<BankAccount> bankAccounts = getBankAccounts();
        for (BankAccount bankAccount : bankAccounts) {

            // 根据银行卡号查找到银行卡
            if (bankAccount.getAccountNumber().equals(accountNumber)) {
                return bankAccount;
            }
        }
        return null;
    }

    /**
     * 获取银行卡列表
     *
     * @return 银行卡列表
     */
    private List<BankAccount> getBankAccounts() {
        return BankAccount.list;
    }

}
